<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="90" formType="add" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      :datakey="'rows'"
      columnsRef="finance_repairFinance_receivables_receiptsDetail_writeOff"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog(true)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { addRole } from '@/api/common/auth/roleManage';
import { getEmployee, receive, getReceiveDetail } from '@/api/finance/repair/receivables';
import * as types from '@/api/common/auth/roleManage/types';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';
import { getAllUser } from '@/api/finance/advancesReceived/register';
import { arrayObjDeWeight } from '@/utils';

export default {
  name: 'WriteOff',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      index: null,
      formList: this.createFormList(),
      columns: this.createTableColumns(),
      dataId: null
    };
  },
  computed: {
    ...mapState('app', ['btnLoading']),
    addBtnState() {
      return this.btnLoading[types.YAN_ROLE_ADD];
    }
  },
  mounted() {
    this.serviceConsultant(); // 服务顾问、经手人下拉
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'LICENSE',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'VIN',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车主编号',
          fieldName: 'OWNER_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'OWNER_NAME',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'RO_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '备件销售单号',
          fieldName: 'SALES_PART_NO',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '结算单号',
          fieldName: 'BALANCE_NO',
          disabled: true
        },
        {
          type: 'SELECT',
          itemList: [],
          label: '服务顾问',
          fieldName: 'SERVICE_ADVISOR',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" disabled={props.row.WRITEOFF_TAG == 10041001} type="text" onClick={() => this.writeOff(props.row)}>
                  销账
                </el-button>
              </div>
            );
          }
        },
        {
          title: '收款单号',
          dataIndex: 'RECEIVE_NO',
          sorter: true
        },
        {
          title: '收款日期',
          dataIndex: 'RECEIVE_DATE',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '收款金额',
          dataIndex: 'RECEIVE_AMOUNT',
          precision: 2,
          sorter: true
        },
        {
          title: '销帐标志',
          dataIndex: 'WRITEOFF_TAG',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.WRITEOFF_TAG, '1004')}</span>;
          }
        },
        {
          title: '经手人',
          dataIndex: 'HANDLER',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '付款方式',
          dataIndex: 'PAY_TYPE_CODE',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.PAY_TYPE_CODE, '8044')}</span>;
          }
        },
        {
          title: '支票号码',
          dataIndex: 'CHECK_NO',
          sorter: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true
        }
      ];
    },
    openModel(data, table) {
      // console.log("传入",data[0],table)
      this.dataId = data[0].BALA_PAYOBJ_ID;
      for (let [key, value] of Object.entries(data[0])) {
        this.formList.some(item => {
          if (item.fieldName === key) {
            item.initialValue = value;
            return true;
          }
        });
      }
      this.$refs.table.$refs.pageTable.EXECUTE_INSERT(table);
      // this.getReductionRecord(data.BALA_PAYOBJ_ID, data.OWNER_CODE )
      // this.getReceiptsDetail(data.BALANCE_NO, data.OWNER_CODE )
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async serviceConsultant() {
      const res = await getAllUser();
      // console.log(res, '服务顾问、经手人');
      let salerData = arrayObjDeWeight(res.data, 'userId');
      this.formList.find(item => item.fieldName === 'SERVICE_ADVISOR').itemList = this.columns.find(item => item.dataIndex === 'HANDLER').editItems = salerData.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    // async saveDialog() {
    //   this.$refs.formPanel.SUBMIT_FORM();

    //   this.$set(this.saveData, 'dataType', '10461001')
    //   this.$set(this.saveData, 'dataSource', '10451001')
    //   if (this.saveData !== null) {
    //     const res = await addRole(this.saveData);
    //     if (res.resultCode === 200) {
    //       this.$notify.success({title: '提示信息',message:'新增成功!'});
    //       // 重新加载
    //       this.closeDialog('refresh');
    //     }else {
    //       this.$notify.error({title: '提示信息',message:res.errMsg||'新增失败!'});
    //       this.closeDialog()
    //     }
    //   }
    // },
    async writeOff(data) {
      this.$confirm('是否确认销账?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await receive(data.RECEIVE_NO);
          console.log('res', res);
          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: '销账成功!' });
            this.$refs.table.$refs.pageTable.EXECUTE_DELETE(this.$refs.table.$refs.pageTable.dataSource);
            this.getReceiveDetail();
          }
        })
        .catch(() => {});
    },
    async getReceiveDetail() {
      const res = await getReceiveDetail(this.dataId);
      // console.log("res",res)
      if (res.resultCode == 200) {
        this.$refs.table.$refs.pageTable.EXECUTE_INSERT(res.data);
      }
    },
    closeDialog(val) {
      this.$emit('close', val);
    }
  }
};
</script>

<style lang="less" scoped></style>
